﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.IO;

namespace Nauka.engine
{
    class HTMLGenerator
    {
        private MainProgram mp;
        private long game_id;
        private string html_string;

        public HTMLGenerator(long game_id, MainProgram mp)
        {
            this.game_id = game_id;
            this.mp = mp;
        }


        public HTMLGenerator(MainProgram mp)
        {
            this.mp = mp;
        }

        private string get_picture(string image){
            string s = "";
            char ch = '"';
            s += "<img src=" + ch + Environment.CurrentDirectory + @"\images\" + image + ch + " />";

            return s;
        }

        private string get_html_card_view(Card c)
        {
            string s = Rank_string.getString(c.rank);
            char ch = '"';
            s += "<img src=" + ch + Environment.CurrentDirectory + @"\images\" + Card.getColorString(c) + ".png" + ch + " />";

            return s;
        }

        public string generate_html_string(string hero)
        {
            string htmls = "<html><body>";

            bool round = false;
            
            Hand hand = mp.dbconn.getHand(this.game_id);
            GamePlayer hero_gp = new GamePlayer();

            htmls += "<b>FL " + hand.getSB() + "/" + hand.getBB() + " - Game No " + hand.getGameNr() + "</b><br><br><b>Known players:</b><br>";
            
            ArrayList game_players = hand.getGamePlayers();
            foreach (GamePlayer gp in game_players)
            {
                if (gp.getNickname() != hero)
                {
                    htmls += Convert.ToString(Position_string.getString(gp.getPosition())) + ". " + gp.getNickname() + " ($" + gp.getAmount() + ")<br>";
                }
                else{
                    htmls += Convert.ToString(Position_string.getString(gp.getPosition())) + ". <b>" + gp.getNickname() + "</b> ($" + gp.getAmount() + ") (Hero)<br>";
                    hero_gp = gp;
                }
            }
            htmls += "<br><b>Preflop</b>: Hero is " + (Position_string.getString(hero_gp.getPosition())) + " with " 
                  + get_html_card_view(hero_gp.getCard(1)) + " " + get_html_card_view(hero_gp.getCard(2)) + "<br>";

            ArrayList game_actions = hand.getActions();
            foreach (Action a in game_actions)
            {
                if (a.getRound().Trim() == "preflop")
                {
                    if(a.getAction().Trim() == "bets" || a.getAction().Trim() == "raises")
                        htmls +=  "<font color=#FF0000>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</font>, ";
                    else if (a.getAction().Trim() == "folds")
                        htmls += "<font color=#C0C0C0><i>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</i></font>, ";
                    else
                        htmls += a.getPlayerNN() + " " + a.getAction().Trim() + ", ";
                }
                if (a.getRound().Trim() == "flop")
                    round = true;
            }

            if (round == true)
            {
                htmls += "<br><br><b>Flop</b>: " + get_html_card_view(new Card(hand.getCard(1))) + " "
                                             + get_html_card_view(new Card(hand.getCard(2))) + " "
                                             + get_html_card_view(new Card(hand.getCard(3))) + "<br>";
                                                                
                round = false;
            }
            foreach (Action a in game_actions)
            {
                if (a.getRound().Trim() == "flop")
                {
                    if (a.getAction().Trim() == "bets" || a.getAction().Trim() == "raises")
                        htmls += "<font color=#FF0000>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</font>, ";
                    else if (a.getAction().Trim() == "folds")
                        htmls += "<font color=#C0C0C0><i>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</i></font>, ";
                    else
                        htmls += a.getPlayerNN() + " " + a.getAction().Trim() + ", ";

                }
                if (a.getRound().Trim() == "turn")
                    round = true;
            }
            if (round == true)
            {
                htmls += "<br><br><b>Turn</b>: " + get_html_card_view(new Card(hand.getCard(4))) + "<br>";
                round = false;
            }

            foreach (Action a in game_actions)
            {
                if (a.getRound().Trim() == "turn")
                {
                    if (a.getAction().Trim() == "bets" || a.getAction().Trim() == "raises")
                        htmls += "<font color=#FF0000>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</font>, ";
                    else if (a.getAction().Trim() == "folds")
                        htmls += "<font color=#C0C0C0><i>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</i></font>, ";
                    else
                        htmls += a.getPlayerNN() + " " + a.getAction().Trim() + ", ";
                }
                if (a.getRound().Trim() == "river")
                    round = true;
            }
            if (round == true)
            {
                htmls += "<br><br><b>River</b>: " + get_html_card_view(new Card(hand.getCard(5))) + "<br>";
                round = false;
            }
            ArrayList river_players = new ArrayList();
            foreach (Action a in game_actions)
            {
                if (a.getRound().Trim() == "river")
                {
                    if (a.getAction().Trim() == "bets" || a.getAction().Trim() == "raises")
                        htmls += "<font color=#FF0000>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</font>, ";
                    else if (a.getAction().Trim() == "folds")
                        htmls += "<font color=#C0C0C0><i>" + a.getPlayerNN() + " " + a.getAction().Trim() + "</i></font>, ";
                    else
                        htmls += a.getPlayerNN() + " " + a.getAction().Trim() + ", ";
                    
                    river_players.Add(a.getPlayerNN());
                }
            }

            round = false;
            htmls += "<br><br><b>Result</b>:<br>";
            foreach (GamePlayer gp in game_players)
            {
                if (gp.getNickname() != hero)
                {
                    try
                    {
                        if (gp.getCard(1).intValue() > 0)
                            htmls += gp.getNickname() + " shows " + get_html_card_view(gp.getCard(1)) + " " + get_html_card_view(gp.getCard(2)) + "<br>";
                        round = true;
                    }
                    catch { }
                }
            }
            if (round == true)
            {
                htmls += "<br>";
            }

            foreach (GamePlayer gp in game_players)
            {
                if (gp.getWon() > 0)
                {
                    htmls +=  gp.getNickname() + " wins  $" + gp.getWon();
                    try
                    {
                        if (gp.getCard(1).intValue() > 0)
                            htmls += " with " + get_html_card_view(gp.getCard(1)) + " " + get_html_card_view(gp.getCard(2));
                    }
                    catch { }
                }
            }


            htmls += "</body></html>";
            Console.WriteLine(htmls);
            return htmls;
        }

        public string getHTMLPlayer(string nickname){
            string s = "<html><body>";
            float vpip = 0, af = 0, wts = 0, pfr = 0;
            DataTable dt = mp.dbconn.getHtmlPlayerStats(nickname);
            DataRow dr = dt.Rows[0];
            try
            {
                string type = "";
                int hands = Convert.ToInt16(dr[0]);
                vpip = (float)Convert.ToDouble(dr[1]);
                pfr = (float)Convert.ToDouble(dr[2]);
                af = (float)Convert.ToDouble(dr[3]);
                wts = (float)Convert.ToDouble(dr[4]);

                s += "<font size=15><center><b>" + nickname + "</b></center></font><br><br>";
                s += "<table><tr align=center><td width=600>";
                s += "<font size=5><b>Preflop: </b></font><br><p>";
                s += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Voluntary Put $ In Pot = " + vpip + " - ";
                //Preflop
                if (vpip < 10)
                {
                    s += "<font color=#FF0000><b> SUPER TIGHT </b></font><br></p>";
                    type += "t";
                }
                else if (vpip < 24)
                {
                    s += "<font color=#FF9900><b> TIGHT </b></font><br></p>";
                    type += "t";
                }
                else if (vpip < 40)
                {
                    s += "<font color=#FFFF00><b> LOOSE </b></font><br></p>";
                    type += "l";
                }
                else
                {
                    s += "<font color=#00FF00><b> VERY LOOSE </b></font><br></p>";
                    type += "l";
                }

                s += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PreFlop Raise = " + pfr + " - ";
                //Preflop
                if (pfr < 6)
                {
                    s += "<font color=#FF0000><b> PASSIV </b></font><br></p>";
                    type += "p";
                }
                else if (pfr < 10)
                {
                    s += "<font color=#FF9900><b> SEMI-AGGRESSIV </b></font><br></p>";
                    type += "p";
                }
                else if (pfr < 20)
                {
                    s += "<font color=#FFFF00><b> AGGRESIV </b></font><br></p>";
                    type += "a";
                }
                else
                {
                    s += "<font color=#00FF00><b> VERY AGGRESIV </b></font><br></p>";
                    type += "a";
                }
                s += "<font size=5><b>Postflop: </b></font><br><p>";
                s += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Aggression Factor = " + af + " - ";
                if (af < 1.0)
                {
                    s += "<font color=#FF0000><b> PASSIV </b></font><br></p>";
                    type += "p";
                }
                else if (af < 1.7)
                {
                    s += "<font color=#FF9900><b> SEMI-AGGRESSIV </b></font><br></p>";
                    type += "a";
                }
                else if (af < 2.9)
                {
                    s += "<font color=#FFFF00><b> AGGRESIV </b></font><br></p>";
                    type += "a";
                }
                else
                {
                    s += "<font color=#00FF00><b> VERY AGGRESIV </b></font><br></p>";
                    type += "a";
                }
                s += "<font size=5><b>Showdown: </b></font><br><p>";
                s += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Went To Showdown = " + wts + " - ";
                if (wts < 31)
                {
                    s += "<font color=#FF0000><b> SELDOM </b></font><br></p>";
                }
                else if (wts < 37)
                {
                    s += "<font color=#FF9900><b> NORMAL </b></font><br></p>";
                }
                else if (wts < 43)
                {
                    s += "<font color=#FFFF00><b> OFTEN </b></font><br></p>";
                }
                else
                {
                    s += "<font color=#00FF00><b> VERY OFTEN </b></font><br></p>";
                }
                s += "</td><td>";
                string image;
                switch (type)
                {
                    case "taa":
                        image = "shark.jpg";
                        break;
                    case "tap":
                        image = "redfish.jpg";
                        break;
                    case "tpa":
                        image = "rock.jpg";
                        if (pfr < 8 && vpip < 10)
                            image = "wall.jpg";
                        break;
                    case "tpp":
                        image = "mouse.jpg";
                        break;
                    case "lpp":
                        if (wts > 50)
                            image = "cs.jpg";
                        else
                            image = "fish.jpg";
                        break;
                    case "laa":
                        if (wts < 50)
                            image = "bomb.jpg";
                        else
                            image = "cs.jpg";
                        if (vpip > 40 && pfr > 20)
                            image = "maniac.jpg";
                          break;
                    case "lap":
                          image = "fish.jpg";
                          break;
                    default:
                          image = "unknown.gif";
                          break;

                }
                s += get_picture(image);
                s += "</td></tr></table>";
            }
            catch (Exception)
            {
                s += "Error";
            }
            s += "</body></html>";
            return s;
        }
    }
}
